package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private RoomInfoService roomInfoService;


    @Override
    public void findSaveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //判断更新或新增
        boolean isUptate = apartmentSubmitVo.getId() != null && apartmentSubmitVo.getId() != 0;
        //保存或更新公寓信息
        this.saveOrUpdate(apartmentSubmitVo);
        //获取公寓id
        Long apartmentId = apartmentSubmitVo.getId();
        //修改功能
        if (isUptate) {
            //1、删除图片信息
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId, apartmentId);
            graphInfoService.remove(graphQueryWrapper);

            //2、删除配套信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

            //3、删除标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLabelQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLabelQueryWrapper);

            //4、删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
        }
        //新增杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(feeValueId).build();
                apartmentFeeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
        //新增配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }
        //新增标签信息
        List<Long> labelInfoIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            for (Long labelInfoId : labelInfoIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentId).labelId(labelInfoId).build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
        //新增图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(graphVoList)){
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentId);
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
    }

    @Override
    public IPage<ApartmentItemVo> findApartmentInfoVoPage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.findApartmentItemVoPage(page, queryVo);
    }

    @Override
    public ApartmentDetailVo findApartmentDetailVoById(Long apartmentId) {
        //查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        //获取公寓所对应的图片信息
        List<GraphVo> graphVoList = graphInfoMapper.findGraphVoListByItemTypeAndItemId(ItemType.APARTMENT,apartmentId);
        //获取公寓所对应的配套信息
        List<FacilityInfo> facilityInfoList = apartmentFacilityMapper.findFacilityInfoListByApartmentId(apartmentId);
        //获取公寓所对应的标签信息
        List<LabelInfo> labelInfoList = apartmentLabelMapper.findLabelInfoListByApartmentId(apartmentId);
        //获取公寓所对应的杂费信息
        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.findFeeValueVoListByApartmentId(apartmentId);
        //创建ApartmentDetailVo对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //复制
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        //赋值
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentInfoById(Long id) {
        //判断公寓中房间的数量
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoService.count(queryWrapper);
        //判断公寓下是否有房间
        if(count > 0){

            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);

        }

        //公寓下没有房间，删除公寓相关的配套、标签、图片、杂费
        //1、删除图片信息
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2、删除配套信息
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

        //3、删除标签信息
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLabelQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLabelQueryWrapper);

        //4、删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);

        //5、删除公寓信息
        this.removeById(id);
    }
}





